/**
 *  模态操作框 
 */
import 'package:flutter_benchu/utils/commonPkg.dart';

class Option<T> {
  String text;
  IconData icon;
  Color iconColor;

  T value;
  void Function(T) onTap;
  Option({this.text, this.value, this.onTap, this.icon, this.iconColor})
      : assert(value != null),
        assert(text != null),
        assert(onTap != null);
}

// ignore: must_be_immutable
class Tooltips<T> extends Dialog {
  final List<Option<T>> options;
  final double top;
  final double right;
  Tooltips(
    this.options, {
    this.top = 0,
    this.right = 0,
  }) : assert(options.length > 0);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.deferToChild,
      onTap: () => Navigator.pop(context),
      child: Material(
        //创建透明层
        type: MaterialType.transparency, //透明类型
        child: Align(
          alignment: Alignment.topRight,
          child: Stack(
            children: <Widget>[
              Positioned(
                top: top - 3,
                right: right + 11,
                child: Image.asset(
                  ImageAssets.sanjiaoxing
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: top, right: right),
                padding: EdgeInsets.symmetric(horizontal: 8.w),
                width: 130.w,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(3))),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children:
                      options.map((option) => TooltipItem<T>(option)).toList(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class TooltipItem<T> extends StatelessWidget {
  final Option<T> option;

  TooltipItem(this.option);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        Navigator.of(context).pop();
        option.onTap(option.value);
      },
      child: Container(
        height: 46.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            border:
                Border(bottom: BorderSide(color: LightColors.divider, width: 0.5))),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SizedBox(width: 10.w),
            Icon(
              option.icon,
              size: 18.w,
              color: option.iconColor ?? Colors.black,
            ),
            SizedBox( width: 11.w),
            Expanded(
              child: Text(
                option.text,
                style: TextStyle(
                    color: LightColors.color_333333,
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w400),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
